package com.example.yogurtsystem.controller;


import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.dto.SaveOrUpdateDto;
import com.example.yogurtsystem.service.BudgetService;
import com.example.yogurtsystem.service.UserRecordService;
import com.example.yogurtsystem.utils.TableTagBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;


/**
 * <p>
 *
 * </p>
 *
 * @author ZWYZY
 * @since 2021/8/15
 */
@RestController
@RequestMapping("/userRecord/")
@Validated
@CrossOrigin
public class UserRecordController {
    @Resource
    private UserRecordService userRecordService;

    @Autowired
    private BudgetService budgetService;

    @ApiOperation("分页查询用户点餐记录列表")
    @GetMapping("/selectUserRecordByLimit")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "pageNum", value = "起始页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderDate", value = "哪一天", required = false, dataType = "Date"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "名字", required = false, dataType = "String")})
    public ResultDTO<?> selectUserRecordByLimit(HttpServletRequest request) {
        TableTagBean ttb = TableTagBean.getFromExt(request);
        return ResultDTO.requestSuccess(userRecordService.selectUserRecordByLimit(ttb));
    }

    @ApiOperation("根据点餐人员名字获取最近点的饮料列表")
    public ResultDTO<?> selectDrinksByUserName(@RequestParam(required = false) String name) {
        //todo
//        userRecordService.selectDrinksByUserName();
        // 前端可以通过获取用户名列表选择或者直接输入名字，如果最近没点过则返回的应该是空
        return ResultDTO.requestSuccess();
    }

    @ApiOperation("根据点餐的饮品显示价格")
    public ResultDTO<?> selectPriceByDrinkName(@RequestParam(required = false) String name) {
        //todo（可选）
        // 正常会有价格表的，由于系统简单 可以通过上面的接口加上前端的配合即可显示价格
        return ResultDTO.requestSuccess();
    }

    @ApiOperation("修改点餐饮品")
    @PostMapping("/saveOrUpdateOrder")
    public ResultDTO<?> saveOrUpdateOrder(@Validated @RequestBody SaveOrUpdateDto saveOrUpdateDto) {

        // 总预算
        BigDecimal budget = budgetService.getQuarterBudget(saveOrUpdateDto.getYearQuarter());
        // 统计当前季度已花费的金额
        BigDecimal amountSpent = userRecordService.getAmountSpent(saveOrUpdateDto.getYearQuarter());
        //如果超出预算则直接返回
        if (budget.compareTo(amountSpent)!=1){
            return ResultDTO.response(500,"超出预算");
        }
        userRecordService.saveOrUpdateOrder(saveOrUpdateDto);
        return ResultDTO.requestSuccess("修改饮品成功");
    }

    //    @ApiOperation("提交当前点餐")
//    public ResultDTO<?> submitOrder(){
//
//        // 提交之后就不能修改了，这个接口的逻辑会涉及三张表。
//        // 如果前端是通过输入名字的，不是通过下拉选择的，即系统之前没有录入的则执行插入用户表;
//        // 如果前端是输入饮品名的，不是通过下拉选择的，需要控制价格的必填且执行插入饮品类型表；
//        // 最后插入人员记录表。
//        return ResultDTO.requestSuccess();
//    }
    @ApiOperation("判断当天是否设置了点餐记录生成")
    @GetMapping("/judgeOrder")
    public ResultDTO<?> judgeOrder(@NotNull(message = "点餐日期不能为空")@DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(required = false) String date) {
        return ResultDTO.requestSuccess(userRecordService.judgeOrder(date));
    }

    @ApiOperation("生成当天的默认点餐记录")
    @PostMapping("/setOrder")
    public ResultDTO<?> setOrder(@NotBlank(message = "点餐日期不能为空") @RequestParam String yearQuarter, @Validated @RequestParam String  id) {
        userRecordService.setOrder(yearQuarter, id);
        return ResultDTO.requestSuccess("设置生效");
    }


    @PostMapping("deleteUserRecord")
    @ApiOperation("删除点餐记录")
    public ResultDTO deleteUserRecord(@RequestParam("id") String id){
        userRecordService.deleteUserRecord(id);
        return ResultDTO.requestSuccess("删除饮品成功");
    }

}
